package com.teckteam.ptvsystem.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.teckteam.ptvsystem.controller.CancelWindowListener;
import com.teckteam.ptvsystem.model.ScraperHelper;
import com.teckteam.ptvsystem.model.facade.PTVConstants;
import com.teckteam.ptvsystem.model.facade.PTVModel;
import com.teckteam.ptvsystem.model.facade.PTVSystem;

/**
 * Timetable selection dialog
 *
 */
public class TimetableBrowser extends JDialog
{
   class refreshSelection implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         ButtonModel compareObj = transTypeSel.getSelection();
         
         if (compareObj == trainBtn.getModel())
            populateList(PTVConstants.TRAIN_FLAG);
         else if (compareObj == tramBtn.getModel())
            populateList(PTVConstants.TRAM_FLAG);
         else if (compareObj == busBtn.getModel())
            populateList(PTVConstants.BUS_FLAG);
         
         okBt.setEnabled(true);
      }
   }
   private JLabel        title = new JLabel("Browse Timetable");
   private JPanel        screen = new JPanel(), radioPanel = new JPanel();
   private JComboBox     cmbox  = new JComboBox();
   private JButton       okBt   = new JButton("Open Timetable"),
         cancelBt = new JButton("Close");
   private ButtonGroup   transTypeSel;
   private JRadioButton  trainBtn, tramBtn, busBtn;
   private PTVModel      model;
   private ScraperHelper helper;
   
   private ArrayList<String> trainList, tramList, busList;
   
   /**
    * Constructor for opening a new Timetable Browser window
    */
   public TimetableBrowser(PTVUserInterface frame)
   {
      super(frame);
      this.model = PTVSystem.getInstance();
      this.setSize(new Dimension(800, 160));
      this.setLocationRelativeTo(frame);
      this.setLayout(new BorderLayout());
      this.setResizable(false);
      this.setTitle("Browse departure timetables");
      this.setModal(true);
      
      transTypeSel = new ButtonGroup();
      trainBtn = new JRadioButton("Train");
      tramBtn = new JRadioButton("Tram");
      busBtn = new JRadioButton("Bus");
      
      trainBtn.addActionListener(new refreshSelection());
      tramBtn.addActionListener(new refreshSelection());
      busBtn.addActionListener(new refreshSelection());
      
      // Selection Grouping
      transTypeSel.add(trainBtn);
      transTypeSel.add(tramBtn);
      transTypeSel.add(busBtn);
      
      radioPanel.add(trainBtn);
      radioPanel.add(tramBtn);
      radioPanel.add(busBtn);
      
      okBt.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
            String route = (String) cmbox.getSelectedItem(), selected = cmbox
                  .getSelectedItem().toString();
            TimetableBrowser.this.setVisible(false);
            TimetableDialog dialog = new TimetableDialog(model
                  .getRouteTimetable(selected));
            TimetableBrowser.this.dispose();
            dialog.setFocusableWindowState(true);
         }
      });
      
      cancelBt.addActionListener(new CancelWindowListener(this));
      okBt.setEnabled(false);
      cmbox.setPreferredSize(new Dimension(600, 25));
      cmbox.setEnabled(false);
      
      screen.add(title);
      screen.add(radioPanel);
      screen.add(cmbox);
      screen.add(okBt);
      screen.add(cancelBt);
      
      // Creates a dynamic dropdown list of the train lines
      // for the user to select
      
      trainList = model.getAllGroupedRoutes(PTVConstants.TRAIN_FLAG);
      tramList = model.getAllGroupedRoutes(PTVConstants.TRAM_FLAG);
      busList = model.getAllGroupedRoutes(PTVConstants.BUS_FLAG);
      
      this.getContentPane().add(screen);
      this.setVisible(true);
   }
   
   /**
    * Used for refreshing the combo box of available route timetables.
    */
   private void populateList(int flag)
   {
      ArrayList<String> selectedList = null;
      
      switch (flag) {
         case PTVConstants.TRAIN_FLAG:
            selectedList = trainList;
            break;
         case PTVConstants.TRAM_FLAG:
            selectedList = tramList;
            break;
         case PTVConstants.BUS_FLAG:
            selectedList = busList;
            break;
      }
      
      cmbox.removeAllItems();
      cmbox.setEnabled(true);
      
      for (int i = 0; i < selectedList.size(); i++)
         cmbox.addItem(selectedList.get(i));
   }
}
